import React from 'react';

// 五行相生相克关系
const fiveElementsRelationship = {
    "木": {
        "生": ["火"],
        "克": ["土"]
    },
    "火": {
        "生": ["土"],
        "克": ["金"]
    },
    "土": {
        "生": ["金"],
        "克": ["水"]
    },
    "金": {
        "生": ["水"],
        "克": ["木"]
    },
    "水": {
        "生": ["木"],
        "克": ["火"]
    }
};

// 根据天干获取五行
const getElementByStem = (stem) => {
    const elements = {
        "甲": "木", "乙": "木", "丙": "火", "丁": "火",
        "戊": "土", "己": "土", "庚": "金", "辛": "金",
        "壬": "水", "癸": "水"
    };
    return elements[stem];
};

// 十神关系分析（简单示例，可根据需求扩展）
const analyzeTenGods = (maleDayMaster, femaleDayMaster) => {
    const tenGods = {
        "甲": { "甲": "比肩", "乙": "劫财", "丙": "食神", "丁": "伤官", "戊": "偏财", "己": "正财", "庚": "七杀", "辛": "正官", "壬": "偏印", "癸": "正印" },
        "乙": { "甲": "劫财", "乙": "比肩", "丙": "伤官", "丁": "食神", "戊": "正财", "己": "偏财", "庚": "正官", "辛": "七杀", "壬": "正印", "癸": "偏印" },
        "丙": { "甲": "偏印", "乙": "正印", "丙": "比肩", "丁": "劫财", "戊": "食神", "己": "伤官", "庚": "偏财", "辛": "正财", "壬": "七杀", "癸": "正官" },
        "丁": { "甲": "正印", "乙": "偏印", "丙": "劫财", "丁": "比肩", "戊": "伤官", "己": "食神", "庚": "正财", "辛": "偏财", "壬": "正官", "癸": "七杀" },
        "戊": { "甲": "七杀", "乙": "正官", "丙": "偏印", "丁": "正印", "戊": "比肩", "己": "劫财", "庚": "食神", "辛": "伤官", "壬": "偏财", "癸": "正财" },
        "己": { "甲": "正官", "乙": "七杀", "丙": "正印", "丁": "偏印", "戊": "劫财", "己": "比肩", "庚": "伤官", "辛": "食神", "壬": "正财", "癸": "偏财" },
        "庚": { "甲": "偏财", "乙": "正财", "丙": "七杀", "丁": "正官", "戊": "偏印", "己": "正印", "庚": "比肩", "辛": "劫财", "壬": "食神", "癸": "伤官" },
        "辛": { "甲": "正财", "乙": "偏财", "丙": "正官", "丁": "七杀", "戊": "正印", "己": "偏印", "庚": "比肩", "辛": "劫财", "壬": "伤官", "癸": "食神" },
        "壬": { "甲": "食神", "乙": "伤官", "丙": "偏财", "丁": "正财", "戊": "七杀", "己": "正官", "庚": "偏印", "辛": "正印", "壬": "比肩", "癸": "劫财" },
        "癸": { "甲": "伤官", "乙": "食神", "丙": "正财", "丁": "偏财", "戊": "正官", "己": "七杀", "庚": "正印", "辛": "偏印", "壬": "劫财", "癸": "比肩" }
    };

    const maleTenGod = tenGods[maleDayMaster][femaleDayMaster];
    let tenGodScore = 0;
    if (['正财', '正官', '正印'].includes(maleTenGod)) {
        tenGodScore = 1;
    } else if (['偏财', '七杀', '偏印'].includes(maleTenGod)) {
        tenGodScore = -1;
    }
    return tenGodScore;
};

// 简单神煞分析（示例，可根据需求扩展）
const analyzeGodsAndEvils = () => {
    // 这里只是示例，实际需要详细的神煞规则
    return 0;
};

// 综合分析函数
const comprehensiveAnalysis = (maleResult, femaleResult) => {
    if (!maleResult || !femaleResult) {
        return null;
    }
    const maleDayMaster = maleResult.bazi.split(' ')[2][0];
    const femaleDayMaster = femaleResult.bazi.split(' ')[2][0];

    const maleElement = getElementByStem(maleDayMaster);
    const femaleElement = getElementByStem(femaleDayMaster);

    let compatibilityScore = 0;

    // 五行生克分析
    if (fiveElementsRelationship[maleElement].生.includes(femaleElement)) {
        compatibilityScore += 1;
    } else if (fiveElementsRelationship[maleElement].克.includes(femaleElement)) {
        compatibilityScore -= 1;
    }

    // 十神关系分析
    compatibilityScore += analyzeTenGods(maleDayMaster, femaleDayMaster);

    // 神煞分析
    compatibilityScore += analyzeGodsAndEvils();

    let isCompatible = false;
    let reason = '';
    if (compatibilityScore > 0) {
        isCompatible = true;
        reason = '综合五行生克、十神关系和神煞分析，两人八字较为合适。';
    } else if (compatibilityScore < 0) {
        isCompatible = false;
        reason = '综合五行生克、十神关系和神煞分析，两人八字可能不太合适。';
    } else {
        isCompatible = true;
        reason = '综合五行生克、十神关系和神煞分析，双方八字无明显优劣，可认为较为合适。';
    }

    return {
        isCompatible,
        reason
    };
};

const BaziCompatibilityAnalysis = ({ maleResult, femaleResult }) => {
    const compatibilityResult = comprehensiveAnalysis(maleResult, femaleResult);

    if (!compatibilityResult) {
        return null;
    }

    return (
        <div
            style={{
                marginTop: "30px",
                padding: "20px",
                backgroundColor: compatibilityResult.isCompatible? "#d4edda" : "#f8d7da",
                borderRadius: "12px"
            }}
        >
            <h2 style={{ textAlign: "center", marginBottom: "20px", fontSize: "20px", fontWeight: "600" }}>八字合婚结果</h2>
            <p style={{ fontSize: "18px", marginBottom: "15px", fontWeight: "500", textAlign: "center" }}>
                {`${maleResult.name}（男）与 ${femaleResult.name}（女）`}
            </p>
            <p style={{ fontSize: "18px", marginBottom: "15px", fontWeight: "500", textAlign: "center" }}>
                {compatibilityResult.isCompatible? "两人八字较为合适" : "两人八字可能不太合适"}
            </p>
            <p style={{ fontSize: "16px", textAlign: "center" }}>{compatibilityResult.reason}</p>
        </div>
    );
};

export default BaziCompatibilityAnalysis;
